﻿using Humanizer;
using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.Process;
using MVC系统mvc.Dto.Quality;
using Newtonsoft.Json;
using System.Reflection;
using WMS_MAV;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using MVC系统mvc.Dto.ProcessProceduresDto;


namespace MVC系统mvc.Controllers
{
    public class ProcessController : Controller
    {
        private readonly IConfiguration _configuration;

        public ProcessController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 显示工艺管理-工艺视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowProcess()
        {
            return View();
        }
        
        /// <summary>
        /// 新增工艺管理-工艺视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProcess()
        {
            return View();
        }
        /// <summary>
        /// 修改工艺管理-工艺视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateProcess()
        {
            return View();
        }
        /// <summary>
        /// 查看工艺管理-工艺视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowPro()
        {
            return View();
        }
        /// <summary>
        /// 反填工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReverseGetByidProcess(GetByid dto)
        {
       
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.token);
            //string url = "http://localhost:22219/api/ProcessRoute/ReverseGetByidProductionProcess?ProductionProcessesId=" + dto.productionProcessesId;
            string url = _configuration["Url"] +"ppapi/api/ProcessRoute/ReverseGetByidProductionProcess?ProductionProcessesId=" + dto.productionProcessesId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 根据质检组ID显示质检项目
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetProList(int QualitygroupId,string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "ppapi/api/QualityPian/GetPList?QualitygroupId=" + QualitygroupId;
            //string url = _configuration["Url"] +"ppapi/api/ProcessRoute/ReverseGetByidProductionProcess?ProductionProcessesId=" + dto.productionProcessesId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 工艺显示
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> ProcessList(ListProcessDto model)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);
      
            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/GetProcessRouteList?ProcessPouteNumber=" + model.ProcessPouteNumber + "&ProcessRouteName="
                + model.ProcessRouteName + "&State=" + model.State + "&PageIndex=" + model.PageIndex+ "&PageSize="+model.PageSize;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 工序数据显示
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetProductiList(ShowProcessProceduresDto dto)
        {
       
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/ShowProductionProcessList?ProductionProcessesName=" + dto.ProductionProcessesName + "&ProductionProcessesNumber=" + dto.ProductionProcessesNumber + "&Status=" + dto.Status + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var data = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 删除工艺信息
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProcessDelete(DeleteProces model)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);

            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/DeleteProcessRoute?ProcessRouteId=" + model.ProcessRouteId;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url,model.ProcessRouteId.ToString());
            return Json(response);

        }
        /// <summary>
        /// 批量删除工艺信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProcessDeleteAll(DeleteAllProcess dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.token);
            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/DeleteAllProcessRoute";
            var response = await client.PostAsync(url, dto.Data);
            return Json(response);

        }
        /// <summary>
        /// 工艺反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetFanAllProcess(string token, int ProcessRouteId)
        {

            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/Handle?ProcessRouteId=" + ProcessRouteId;
            //string url = _configuration["Url"] + "/warehouseRead/api/Allocation/GetAllocationProductModelList?AllocationId=" + allocationId;
            var response = await client.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 工艺信息修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetUpdateProcess(UpdateProcess field, string token)
        {

            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "ppapi/api/ProcessRoute/UpdateProcessRoute";
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 添加工艺信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetProcessAdd(CreateProcess field, string token)
        {
            //http://localhost:22219
            HttpClientWrapper client = new HttpClientWrapper(token);
            //string url ="http://localhost:22219/api/ProcessRoute/CreateProcessRoute";
            string url = _configuration["Url"]+ "ppapi/api/ProcessRoute/CreateProcessRoute";
            string list = JsonConvert.SerializeObject(field);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
    }
}
